//https://leetcode.cn/problems/max-consecutive-ones-iii/

class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int left = 0, right = 0, len = 0, count = 0;
        int n = nums.size();

        while (right < n)
        {
            //进窗口
            if (nums[right] == 0)
            {
                count++;
            }
            //判断
            if (count > k)
            {
                //出窗口
                if (nums[left++] == 0)
                    count--;
            }
            //更新结果
            len = max(len, right - left + 1);

            right++;
        }

        return len;

    }
};